/*
 *	Filename: wuyaowei.c
 *	 Version: 0.1.0
 *   Description: josephus solution
 *
 *        Author: wuyaowei
 *          Date: 2020-06-01
 *
 */

#include <stdio.h>
#include <stdlib.h>

static int josephus_solution(int n, int m) {
    int step = 0;
    int num[n];
    int counter = 0; //记录出局人数
    int sub = 0;     //数组下标

    if (n <= 0 || m <= 0) {
        return -1;
    }

    for (int i = 0; i < n; i++) {
        num[i] = 0;   //初始化数据
    }

    for (int i = 0; ; i= (i + 1) % n) {
        if (num[i] == -1) {
	    continue;
	}

	step++;
	if (step == m) {
	    num[i] = -1;
	    step = 0;
	    counter++;
	   // printf("The num %d out! \n", i+1);
	}
	if (counter == n) {
	    return i+1;
	}
    }
}

void main() {
    int n = 0, m = 0;
    char str[5];

    printf("Please input total num:\n");
    fgets(str, 5, stdin);
    n = atoi(str);
    printf("Please input counter:\n");
    fgets(str, 5, stdin);
    m = atoi(str);
    printf("Run result: %d\n", josephus_solution(n,m));
}
